﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HostpitalDevice.Business;
using System.Collections;
namespace HostpitalDevice
{
    public partial class ChooseForm : Form
    {
        //员工的全部属性值
        private string[] stringlist ={"姓名","性别","民族","出生年月","政治面貌",
                                "来本单位时间","退休时间","参加工作时间","身份证号",
                                "最高学历","毕业专业","毕业学校","第一学历","毕业-学校","毕业-专业",
                                "聘任专业职务","个人身份","专业资格","级别",
                                "户籍所在地","是否签订合同","是否新人","岗位类别与名称","所在科室","岗位状态",
                                "是否参加社区岗位培训","岗位培训","培训证书编号","岗位上岗证","岗位上岗号",
                                "人员性质","是否双肩挑","缴纳保险类型","是否缴纳住房公积金",
                                "所在单位","是否外聘",
                                "合同类型","是否劳务派遣","原工作单位","退休单位",
                                "现存档单位","缴费单位","户口类型"};

        /// <summary>
        /// 选择的数据集合 
        /// </summary>
        private  string m_choicesList;
        public string  ChoicesList
        {
            get { return m_choicesList; }
            set { m_choicesList = value; }
        }

        /// <summary>
        /// 是否要导入数据
        /// </summary>
        private bool m_isLoad;
        public bool IsLoad
        {
            get { return m_isLoad; }
            set { m_isLoad = value; }
        }

        /// <summary>
        /// 已经选择了的数据集合
        /// </summary>
        private string m_txtValue;
        public string TxtValue
        {
            get { return m_txtValue; }
            set { m_txtValue = value; }
        }

        /// <summary>
        /// 打印员工的属性列表
        /// </summary>
        private ArrayList m_propertyList;
        public ArrayList PropertyList
        {
            get { return m_propertyList; }
            set { m_propertyList = value; }
        }

        /// <summary>
        /// 初始化对话框，包括标题，里面的数据
        /// </summary>
        /// <param name="type"></param>
        /// <param name="isnew"></param>
        /// <param name="txtValue"></param>
        public ChooseForm(string type, bool isnew, string txtValue)
        {
            InitializeComponent();
            this.Text = type;
            m_choicesList = "";
            m_isLoad = !isnew;
            m_txtValue = txtValue;
            m_propertyList = new ArrayList();
        }

        /// <summary>
        /// 判断part是否是total的一部分
        /// </summary>
        /// <param name="total">总的字符串</param>
        /// <param name="part">子字符串</param>
        /// <returns></returns>
        private bool CheckIndexOf(string total, string part)
        {
            char[] separator ={ ';' };
            String[] splitStrings = new String[20];
            splitStrings = total.Split(separator);
            int i = 0;
            while (i < splitStrings.Length)
            {
                if (part.Equals(splitStrings[i]) == true)
                    return true;
                else
                    i++;
            }
            return false;
        }

        /// <summary>
        /// 向对话框中导入数据集合
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChooseForm_Load(object sender, EventArgs e)
        {
            if (this.Text.Equals("员工属性列表"))
            {
                for (int j = 0; j < stringlist.Length; j++)
                {
                    this.checkedList.Items.Add(stringlist[j], true);
                }
                return;
            }

            if (this.Text.Equals("选择单位"))
            {
                this.btnExit.Enabled = IsLoad;
                Company company = CompanyManage.GetCompanies();
                foreach (Company c in company.Companies)
                {
                    this.checkedList.Items.Add(c.Name);
                }
                return;
            }

            DataTable result=SettingManage.GetAllValueByType (this.Text .ToString ());
            if (result.Rows.Count > 0)
            {
                for (int i = 0; i < result.Rows.Count; i++)
                {
                    if (IsLoad &&CheckIndexOf (TxtValue ,result.Rows[i][0].ToString ()))// (TxtValue.IndexOf(result.Rows[i][0].ToString()) != -1))
                        this.checkedList.Items.Add(result.Rows[i][0].ToString(), true);
                    else
                        this.checkedList.Items.Add(result.Rows[i][0].ToString());
                }
            }
        }

        /// <summary>
        /// 提交选择了的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.Text.Equals("员工属性列表"))
            {
                foreach (object checkedItem in this.checkedList.CheckedItems)
                {
                    m_propertyList.Add(checkedItem.ToString());
                }
                return;
            }
            if (this.Text.Equals("选择单位") == true && this.checkedList.CheckedItems.Count > 1)
            {
                MessageBox.Show("只能选择一个单位");
                return;
            }
            if (this.Text.Equals("人员性质") && this.checkedList.CheckedItems.Count > 1)
            {
                MessageBox.Show("只能选择一个,不能多选");
                return;
            }

            foreach (object checkedItem in this.checkedList.CheckedItems)
            {
                if(this.checkedList.CheckedItems.Count ==1)
                    m_choicesList +=checkedItem.ToString ();
                else
                    m_choicesList += checkedItem.ToString() + ";";
            }
            this.Close();
        }

        /// <summary>
        /// 返回，没有选择数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}